package com.ruoyi.web.controller.form;

import java.util.List;

import com.ruoyi.form.domain.FormRegister;
import com.ruoyi.form.service.IFormRegisterService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.form.domain.FormLog;
import com.ruoyi.form.service.IFormLogService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 接口调用日志Controller
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Controller
@RequestMapping("/form/log")
public class FormLogController extends BaseController
{
    private String prefix = "form/log";

    @Autowired
    private IFormLogService formLogService;
    @Autowired
    private IFormRegisterService registerService;

//    @RequiresPermissions("form:log:view")
    @GetMapping()
    public String log()
    {
        return prefix + "/log";
    }

    /**
     * 查询接口调用日志列表
     */
//    @RequiresPermissions("form:log:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FormLog formLog)
    {
        startPage();
        List<FormLog> list = formLogService.selectFormLogList(formLog);
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(e->{
                Integer registerId = e.getRegisterId();
                FormRegister formRegister = registerService.selectFormRegisterById(registerId);
                if(formRegister != null){
                    e.setName(formRegister.getName());
                    e.setPhone(formRegister.getUserPhone());
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出接口调用日志列表
     */
//    @RequiresPermissions("form:log:export")
    @Log(title = "接口调用日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FormLog formLog)
    {
        List<FormLog> list = formLogService.selectFormLogList(formLog);
        ExcelUtil<FormLog> util = new ExcelUtil<FormLog>(FormLog.class);
        return util.exportExcel(list, "接口调用日志数据");
    }

    /**
     * 新增接口调用日志
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存接口调用日志
     */
//    @RequiresPermissions("form:log:add")
    @Log(title = "接口调用日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FormLog formLog)
    {
        return toAjax(formLogService.insertFormLog(formLog));
    }

    /**
     * 修改接口调用日志
     */
//    @RequiresPermissions("form:log:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        FormLog formLog = formLogService.selectFormLogById(id);
        mmap.put("formLog", formLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存接口调用日志
     */
//    @RequiresPermissions("form:log:edit")
    @Log(title = "接口调用日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FormLog formLog)
    {
        return toAjax(formLogService.updateFormLog(formLog));
    }

    /**
     * 删除接口调用日志
     */
//    @RequiresPermissions("form:log:remove")
    @Log(title = "接口调用日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(formLogService.deleteFormLogByIds(ids));
    }
}
